//导入验证js
import WxValidate from "../../util/WxValidate";
//请求的Api接口
import {
  postorder
} from "../../util/api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //数量选择
    num: 0,
    //标题
    title: "",
    // 价格
    price: 0,
    // 日期
    datetime: "",
    // 图片
    commodityImg: [],
    // 总计
    total: 0,
    // 商品列表
    GoodArr: [],
    //地区
    district: "",
     //弹话框显示
     jumpDialog: true,
  },
    //关闭弹出对话框
    canceljumpDialog() {
      this.setData({
        jumpDialog: true
      })
    },
      //跳转登录页面
    jumpLogin(){
      wx.switchTab({
        url: '../personal/personal'
      })
    },
  formSubmit: function (e) {
    let portraitImg = wx.getStorageSync('portrait');
    //判断用户是否授权
    wx.getSetting({ //得到设置
      success: (data) => {
        if (data.authSetting['scope.userInfo']) {
          var orderCode = '';
          for (var i = 0; i < 6; i++) //6位随机数，用以加在时间戳后面。
          {
            orderCode += Math.floor(Math.random() * 10);
          }
          orderCode = "E" + new Date().getTime() + orderCode; //时间戳，用来生成订单号。
          let travel_name = WxValidate('uName', e.detail.value.travel_name); //验证姓名
          let travel_phone = WxValidate('phone', e.detail.value.travel_phone); //验证电话号码
          let travel_Idcard = WxValidate('idCard', e.detail.value.travel_Idcard); //验证身份证号码
          let name = WxValidate('uName', e.detail.value.name); //验证姓名
          let tel = WxValidate('phone', e.detail.value.tel); //验证电话号码
          var signUpName = [];
          for (let index = 0; index < this.data.GoodArr.length; index++) {
            signUpName.push(this.data.GoodArr[index].name);
          }
          var signUpPhone = [];
          for (let index = 0; index < this.data.GoodArr.length; index++) {
            signUpPhone.push(this.data.GoodArr[index].phone);
          }
          var IdCardno = [];
          for (let index = 0; index < this.data.GoodArr.length; index++) {
            IdCardno.push(this.data.GoodArr[index].Idcard);
          }
          var data = wx.getStorageSync('userInfo'); //微信用户唯一ID
      
      
          var state = 0;
          var dataInfo = {
            coverpage: this.data.commodityImg,
            title: this.data.title,
            name: e.detail.value.name,
            phonenumber: e.detail.value.tel,
            ordernumber: orderCode,
            price: this.data.total,
            signUpName: JSON.stringify(signUpName),
            signUpPhone: JSON.stringify(signUpPhone),
            IdCardno: JSON.stringify(IdCardno),
            applicantsnumber: this.data.num,
            orderDate: this.data.datetime,
            district:this.data.district,
            pid: data.open_id,
            state: state
          }
      
          // 表单预验证通过 再调用接口
          if (travel_name && travel_phone && travel_Idcard && name && tel) {
            postorder(
              dataInfo
            ).then((res) => {
              if (res.data.ret == 200) {
                wx.showToast({
                  title: '提交成功，请等待审核',
                  icon: 'success',
                  duration: 2000
                });
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1,
                  })
                }, 3000)
              }
            })
          }

        }else{
          setTimeout(function () {
            this.setData({
              jumpDialog: false
            })
          }.bind(this), 200)
        }
      }
    })
    

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var goodarr = JSON.parse(options.goodarr);
    this.setData({
      num: options.num,
      title: options.title,
      price: options.price,
      datetime: options.datetime,
      commodityImg: options.commodityImg,
      total: options.price * options.num,
      GoodArr: goodarr,
      district: options.district
    });

    var arr = this.data.GoodArr; // 创建一个数组
    for (var i = 0; i < this.data.GoodArr.length; i++) {
      arr[i].name = []; // 添加出游人员姓名
      arr[i].phone = []; // 添加出游人员手机号码
      arr[i].Idcard = []; // 添加出游人员身份证号码
    }
    this.setData({
      GoodArr: arr
    });

  },
  //数组添加出游人员姓名
  Btn_name: function (e) {
    let dataset = e.currentTarget.dataset; // 获取索引
    let value = e.detail.value; // 获取value
    let arr = this.data.GoodArr;
    arr[dataset.index].name = value;

  },

  //数组添加出游人员电话号码
  Btn_phone: function (e) {
    let dataset = e.currentTarget.dataset; // 获取索引
    let value = e.detail.value; // 获取value
    let arr = this.data.GoodArr;
    arr[dataset.index].phone = value;

  },
  //数组添加出游人员身份证号码
  Btn_Idcard: function (e) {
    let dataset = e.currentTarget.dataset; // 获取索引
    let value = e.detail.value; // 获取value
    let arr = this.data.GoodArr;
    arr[dataset.index].Idcard = value;

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})